import {createRouter, createWebHashHistory} from "vue-router";
import Frame from "../views/Frame.vue";
const routes = [
    {
        path: '/',
        redirect: '/index'
    },
    {
        path: "/frame",
        name: "Frame",
        component: Frame,
        children: [
            {
                path: "/index",
                name: "Index",
                meta: {
                    title: '首页'
                },
                component: () => import ("../views/Index.vue")
            },
            {
                path: "/read",
                name: "Read",
                meta: {
                    title: '文章展示'
                },
                component: () => import ("../views/Read.vue")
            },
            {
                path: "/schedule",
                name: "Schedule",
                meta: {
                    title: '日程信息'
                },
                component: () => import ("../views/Schedule.vue")
            }
        ]
    },
    {
        path: "/editor",
        name: "Editor",
        meta: {
            title: '编辑器'
        },
        component: () => import ("../views/Editor.vue")
    },
    {
        path: "/editor-update",
        name: "EditorUpdate",
        meta: {
            title: '文章修改'
        },
        component: () => import ("../views/EditorUpdate.vue")
    },
    {
        path: "/403",
        name: "403",
        meta: {
            title: '403'
        },
        component: () => import ("../views/403.vue")
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    const username = localStorage.getItem('username');
    if (!username && (to.path === '/editor' || to.path === "/schedule")) {
        next('/403');
    } else {
        next();
    }
});

export default router;